﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.UserManageDomain
{
    public class UserInfoService : IUserInfoService
    {
        private readonly ILogUtil _logUtil;
        private readonly IUserInfoRepository _userInfoRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _snowflake;

        public UserInfoService(IUserInfoRepository userInfoRepository, ILogUtil logUtil, IMapper mapper, ISnowflake snowflake)
        {
            _userInfoRepository = userInfoRepository;
            _logUtil = logUtil;
            _Mapper = mapper;
            _snowflake = snowflake;
        }
        public async Task<UserInfoApiDto> LoginAsync(string userName, string password)
        {
            UserInfoApiDto userInfoDto = new UserInfoApiDto();
            //用户名校验
            UserInfo userInfo = await _userInfoRepository.GetSingleAsync(it => it.UserName == userName && it.IsDelete == false);
            if (userInfo == null)
            {
                throw new DomainException("用户名不存在", _logUtil, MethodBase.GetCurrentMethod().DeclaringType.Name + MethodBase.GetCurrentMethod().Name);
            }
            //var st = GenerateMD5.DESEncrypt(password);
            //密码校验 需加密
            if (userInfo.Password != /*GenerateMD5.DESEncrypt(password)*/GenerateMD5.CreateMD5(password))
            {
                throw new DomainException("密码错误", _logUtil, MethodBase.GetCurrentMethod().DeclaringType.Name + MethodBase.GetCurrentMethod().Name);
            }
            _Mapper.Map(userInfo, userInfoDto);//AutoMapper
            return userInfoDto;
        }

        public async Task<bool> InitPasswordAsync(List<string> userNames)
        {
            return await _userInfoRepository.InitPasswordAsync(userNames);
        }

        public async Task<bool> UpdatePasswordAsync(long userID, string newPassword)
        {
            var password= GenerateMD5.DESEncrypt(newPassword);
            return await _userInfoRepository.UpdateAsync(it => new UserInfo() { Password = password }, it => it.UserID == userID);
        }

        public async Task<bool> UpdateUserInfoAsync(long userID, string newPassword,string userName,string jobNumber, DateTime dateTime)
        {
            return await _userInfoRepository.UpdateAsync(it => new UserInfo() 
            {
                Password = newPassword ,
                UserName = userName ,
                JobNumber= jobNumber,
                UpdateTime = dateTime
            }, 
            it => it.UserID == userID);
        }


        public async Task<List<UserInfoApiDto>> GetUserListAsync()
        {
            List<UserInfoApiDto> userInfoDto = new List<UserInfoApiDto>();
            //用户名校验
            List<UserInfo> userInfo = await _userInfoRepository.GetListAsync();
            
            _Mapper.Map(userInfo, userInfoDto);//AutoMapper
            return userInfoDto;
        }




        public async Task<UserInfoApiDto> GetUserByIDAsync(long userID)
        {
            UserInfoApiDto userInfoDto = new UserInfoApiDto();
            //用户名校验
            UserInfo userInfo = await _userInfoRepository.GetFirstAsync(l=>l.UserID == userID);
            _Mapper.Map(userInfo, userInfoDto);//AutoMapper
            userInfoDto.Password = GenerateMD5.DESDecrypt(userInfo.Password);
            return userInfoDto;
        }

        /// <summary>
        /// 添加新用户
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> AddUserAsync(UserInfoApiDto userInfoDto)
        {
            UserInfo userInfo = new UserInfo();
            _Mapper.Map(userInfoDto, userInfo);
            return await _userInfoRepository.InsertAsync(userInfo);

        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public async Task<bool> DeleteUserAsync(long userID)
        {
            return await _userInfoRepository.DeleteAsync(x => x.UserID == userID);
        }



        public void Begin()
        {
            _userInfoRepository.AsTenant().BeginTran();
        }

        public void Commit()
        {
            _userInfoRepository.AsTenant().CommitTran();
        }

        public void RollBack()
        {
            _userInfoRepository.AsTenant().RollbackTran();
        }
    }
}
